package com.ce.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ce.server.common.RespBean;
import com.ce.server.common.RespPageBean;
import com.ce.server.common.StudentVo;
import com.ce.server.pojo.Role;
import com.ce.server.pojo.Student;
import com.ce.server.mapper.StudentMapper;
import com.ce.server.service.IOperlogService;
import com.ce.server.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 郑爽
 * @since 2021-11-17
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {

  @Autowired
  private StudentMapper studentMapper;
  @Autowired
  private IOperlogService operlogService;
  @Value("${type.student}")
  private String student_type;
  

  /**
   * 根据账号查询学生信息
   *
   * @param Sno
   * @return
   */
  @Override
  public RespBean getStudentBySnoVo(String Sno) {
    Student student = studentMapper.selectOne(new LambdaQueryWrapper<Student>().eq(Student::getSno, Sno));
    if (student==null)
      return RespBean.error("该学号不存在！");
    StudentVo studentVo = new StudentVo();
    studentVo.setAddress(student.getAddress()).setAge(student.getAge()).setBirthday(student.getBirthday())
            .setClassInfo(student.getClassInfo()).setGender(student.getGender()).setGrade(student.getGrade())
            .setIdCard(student.getIdCard()).setMajor(student.getMajor())
            .setSname(student.getSname()).setSno(student.getSno()).setNativePlace(student.getNativePlace());

    return RespBean.success("查询成功！",studentVo);
  }

  @Override
  public Student getStudentBySno(String Sno) {
    return studentMapper.selectOne(new LambdaQueryWrapper<Student>().eq(Student::getSno, Sno));
  }

  /**
   * 根据账号查询学生信息
   *
   * @param Sno
   * @return
   */
  @Override
  public List<Role> getRoleBySno(String Sno) {

    return studentMapper.getRoleBySno(Sno);
  }

  /**
   * 分页获取学生信息
   *
   * @param currentPage
   * @param size
   * @param student
   * @return
   */
  @Override
  public RespPageBean getStudentsByPage(Integer currentPage, Integer size, Student student) {
    Page<Student> page = new Page<>(currentPage, size);
    IPage<Student> studentsByPage = studentMapper.getStudentsByPage(page, student);
    if (studentsByPage != null) {
      return new RespPageBean(studentsByPage.getTotal(), studentsByPage.getRecords());
    }
    return null;
  }

  /**
   * 添加学生信息
   *
   * @param student
   * @return
   */
  @Override
  public boolean addStuInfo(Student student) {
    BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
    student.setPass(encoder.encode("123456"));
    student.setUserface("https://checkentry1.oss-cn-guangzhou.aliyuncs.com/2021/11/30/8d42c21754b1440c88669768455d4186.png");
    return studentMapper.insert(student)>0;
  }

  /**
   * 批量删除
   * @param snos
   * @return
   */
  @Override
  public Integer delStuBatch(String[] snos) {
    List<String> list_snos = Arrays.stream(snos).collect(Collectors.toList());
    return studentMapper.deleteBatchIds(list_snos);
  }

  /**
   * 查询所有学生数据
   * @return
   */
  @Override
  public List<Student> getStuRecords() {
    Integer count = studentMapper.selectCount(null);
    Page<Student> studentPage = new Page<>(1, count);
    IPage<Student> studentsByPage = studentMapper.getStudentsByPage(studentPage, new Student());
    return studentsByPage.getRecords();

  }

  /**
   * 获取最大的学号
   * @return
   */
  @Override
  public Map<String,Object> recheckSno(String sno) {
    /*List<Map<String, Object>> mapList = studentMapper.selectMaps(new QueryWrapper<Student>().select("max(sno)"));
    String maxSno = ((String) mapList.get(0).get("max(sno)")).substring(student_type.length());*/
    HashMap<String, Object> map = new HashMap<>();
    Integer count = studentMapper.selectCount(new LambdaQueryWrapper<Student>().eq(Student::getSno, "stu_" + sno));
    if (count==0) {
      map.put("flag", 1);
      return map;
    }
    map.put("m","该学号已存在");
    map.put("flag","0");
    return map;
  }

  /**
   * 更新当前登录学生信息
   * @param student
   * @return
   */
  @Override
  public boolean updateAccountInfo(Student student) {
    if (studentMapper.updateAccountInfo(student))
      return true;
    return false;
  }
}
